<template>
  <div
    class="coupon-item"
    :class="{
      checked: props.currentCoupon.coupon_id === props.it.c_id,
      disabled: !props.it.expireSecond,
    }"
  >
    <div class="amount">
      <em>{{ moneySymbol }}</em>{{ props.it.amount_ }}
      <div class="fs12">现金券</div>
    </div>
    <div class="line" />
    <div class="cont">
      <div class="cont-text">
        <div class="title">
          <span :title="props.it.name">{{ props.it.name }}</span>
        </div>
        <div class="mt5 flex-center" v-if="props.it.expireSecond">
          <time-to :second="props.it.expireSecond" size="small"/>
        </div>
      </div>
      <template v-if="!props.it.expireSecond">
        <div class="btn">已失效</div>
      </template>
      <template v-else-if="props.available">
        <div class="btn" v-if="props.currentCoupon.coupon_id === props.it.c_id">已选择</div>
        <div class="btn" v-else @click="handleCouponChange(props.it)">选择</div>
      </template>
      <div class="btn" v-else @click="handleUseCoupon(props.it)">去使用</div>
    </div>
    <div class="check-icon">
      <el-icon :size="14" color="#fff"><Check /></el-icon>
    </div>
  </div>
</template>
<script setup lang="ts">
import { Check } from "@element-plus/icons-vue";
import TimeTo from "@/components/time-to.vue";
import { defineProps, ref, defineEmits, computed, onMounted } from "vue";
const emit = defineEmits(["switch", "use"]);
const props = defineProps({
  it: {
    type: Object,
    default() {
      return {};
    },
  },
  available: Boolean,
  currentCoupon: {
    type: Object,
    default() {
      return {};
    },
  }
});
const moneySymbol = computed(() => "￥");
// 切换优惠券
function handleCouponChange(info: any) {
  emit("switch", info);
}
// 切换满足优惠券套餐
function handleUseCoupon(info: any) {
  emit("use", info);
}
</script>
<style lang="scss" scoped>
.coupon-item {
  position: relative;
  flex: 0 0 304px;
  display: flex;
  align-items: center;
  min-height: 60px;
  background: linear-gradient(
    180deg,
    rgba(255, 137, 137, 0.1) 0%,
    rgba(255, 75, 75, 0.1) 100%
  );
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 0 0 1px #f84d4d inset;
  .line {
    &::before,
    &::after {
      box-shadow: 0 0 0 1px #f84d4d inset;
    }
  }
  .amount {
    flex: 0 0 75px;
    font-weight: bold;
    font-size: 24px;
    color: #f84d4d;
    text-align: center;
    em {
      font-size: 12px;
      font-weight: bold;
    }
  }
  .line {
    position: absolute;
    top: 0;
    left: 75px;
    height: 100%;
    border-left: 1px dashed #f84d4d;
    &::before,
    &::after {
      content: "";
      position: absolute;
      width: 12px;
      height: 12px;
      background: #f4f8ff;
      border-radius: 50%;
      left: -6px;
    }
    &::before {
      top: -6px;
    }
    &::after {
      bottom: -6px;
    }
  }
  .cont {
    flex: 1;
    padding: 10px 15px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    overflow: hidden;
  }
  .cont-text{
    flex: 1;
    padding-right: 7px;
    overflow: hidden;
  }
  .title {
    line-height: 18px;
    font-weight: bold;
    font-size: 14px;
    color: var(--el-text-color-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .btn {
    flex: 0 0 48px;
    background: #ff2c2c;
    border-radius: 12px;
    font-weight: 400;
    font-size: 12px;
    color: #fff;
    line-height: 16px;
    padding: 2px 2px;
    // word-break: break-all;
    word-wrap: break-word;
    overflow-wrap: break-word;
    -webkit-hyphens: auto;
    -moz-hyphens: auto;
    hyphens: auto;
    text-align: center;
    cursor: pointer;
    &:hover {
      opacity: 0.8;
    }
  }
  .check-icon {
    display: none;
    position: absolute;
    top: 0;
    right: 0;
    width: 42px;
    height: 30px;
    pointer-events: none;
    text-align: right;
    padding-right: 5px;
    &::before {
      content: "";
      position: absolute;
      top: 0;
      right: 0;
      border: 15px solid transparent;
      border-left-width: 21px;
      border-right-width: 21px;
      border-right-color: #0ec457;
      border-top-color: #0ec457;
    }
  }
  &.checked {
    .btn {
      opacity: 0.5;
      cursor: default;
    }
    .check-icon {
      display: block;
    }
  }

  &.disabled {
    box-shadow: unset;
    background: rgba(0, 0, 0, 0.05);
    .amount,
    .title {
      color: var(--el-text-color-secondary);
    }
    .line {
      border-left-color: rgba(0, 0, 0, 0.1);
      &::before,
      &::after {
        box-shadow: unset;
      }
    }
    .btn {
      background: rgba(0, 0, 0, 0.2);
      cursor: no-drop;
    }
    &.checked {
      .check-icon::before {
        border-right-color: rgba(0, 0, 0, 0.2);
        border-top-color: rgba(0, 0, 0, 0.2);
      }
    }
  }
}
</style>
